﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using GameSchoolApplication.Models;

namespace GameSchoolApplication.Repositories
{

    public class LectureRepository
    {
        gsDBDataContext link = new gsDBDataContext();

        /// <summary>
        /// Getting a lecture by an ID
        /// Precondition: An lecture must exist and have an ID
        /// Postcondition: A lecture is returned
        /// Author: Bjarmi mod. by Alma
        /// Date: 05.05. 2012
        /// </summary>
        public Lecture getLectureByID(int lectID)
        {
            Lecture selectedLecture = (from l in link.Lectures
                               where l.lectureID == lectID
                               select l).First();            
            return selectedLecture; 
        }

        /// <summary>
        /// Getting all Lectures
        /// Precondition: Lectures must exist
        /// Postcondition: A list of lectures is returned
        /// Author: Aron/Bjarmi mod. by Alma
        /// Date: 05.05. 2012
        /// </summary>
        public IQueryable<Lecture> getAllLectures()
        {
            return link.Lectures;
        }

        /// <summary>
        /// Adding a new lecture 
        /// Precondition: The user must be a teacher
        /// Postcondition: A new lecture has been added to the database
        /// Author: Aron/Bjarmi mod. by Alma 
        /// Date: 05.05. 2012
        /// </summary>
        public bool addLecture(Lecture myLecture)
        {
            try
            {
                link.Lectures.InsertOnSubmit(myLecture);
                save();
            }
            catch (Exception)
            {
                return false;
            }
            return true; 
        }

        /// <summary>
        /// Removing a lecture
        /// Precondition: Lecture must exist and the user must be admin/ teacher
        /// Postcondition: A lecture has been removed from database
        /// Author: Bjarmi mod. by Alma
        /// Date: 05.05. 2012
        /// </summary>
        public bool removeLecture(int myLectureID)
        {
            Lecture myLecture = (from l in link.Lectures
                               where l.lectureID == myLectureID
                               select l).First();
            if (myLecture != null)
            {
                try
                {                    
                    link.Lectures.DeleteOnSubmit(myLecture);
                }
                catch (Exception)
                {
                    return false;
                }
                return true;
            }
            return false;
        }

        /// <summary>
        /// Editing info abour a lecture
        /// Precondition: User must be an administrator and the lecture must exist
        /// Postcondition: Lecture information have been updated in the database
        /// Author: Aron mod. by Alma
        /// Date: 05.05. 2012
        /// </summary>
        public bool editLecture(Lecture myLecture)
        {
            try
            {
                var lect = (from l in link.Lectures
                            where l.lectureID == myLecture.lectureID
                            select l).First();

                lect.name = myLecture.name;
                lect.lectureDesc = myLecture.lectureDesc;
                lect.filePath = myLecture.filePath;
                lect.lvlID = myLecture.lvlID;
                lect.dateOfRecording = myLecture.dateOfRecording;

                save();

                return true;
            }
            catch (Exception)
            {
                return false;
            }
            
        }

        /// <summary>
        /// Saves changes to database
        /// Precondition:
        /// Postcondition: Database has been updated
        /// Author: Aron og Bjarmi
        /// Date: 03.05.2012
        /// </summary>y>
        public void save()
        {
            link.SubmitChanges();
        }


        /// <summary>
        /// Returns a queryable list of all lectures of the level with the specified ID
        /// Precondition: level must exist
        /// Postcondition: All lectures of the level have been returned
        /// Author: Hlynur
        /// Date: 11.5.12
        /// </summary>
        /// <param name="courseID">The ID of the course</param>
        public IQueryable<Lecture> getLecturesOfLevel(int levelID)
        {
            IQueryable<Lecture> lects = (from a in link.Lectures
                                              where a.lvlID == levelID
                                              select a);
            return lects;
        }



    }
}